﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Tools;

namespace Punia
{
    [CommandAttribute(false)]
    public partial class begin_converation : BaseForm
    {
        public begin_converation()
            : base()
        {
        }

        public begin_converation(DbContext dbContext)
            : base(dbContext)
        {
            InitializeComponent();

            cbServices.DataSource = Utility.services(dbContext);
            cbServices.DisplayMember = "Name";

            cbContract.DataSource = Utility.contracts_to_select(dbContext);
            cbContract.DisplayMember = "Name";

            cbConversation.DataSource = Utility.conversation_groups(dbContext);
            cbConversation.DisplayMember = "Name";

            cbMessage.DataSource = Utility.message_types_to_select(dbContext);
            cbMessage.DisplayMember = "Name";
        }

        protected override void Write()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("BEGIN TRY");
            sb.AppendLine("   BEGIN TRANSACTION");
            sb.AppendLine("      DECLARE @ch UNIQUEIDENTIFIER");
            sb.AppendLine("      DECLARE @msg NVARCHAR(MAX);");
            sb.AppendLine();
            sb.AppendLine("      BEGIN DIALOG CONVERSATION @ch");
            sb.AppendLine("      FROM SERVICE [" + cbServices.Text + "]");
            sb.AppendLine("      TO SERVICE '" + tbTo.Text + "'");

            if (tbGuid.Text != string.Empty)
                sb.AppendLine("      ,'" + tbGuid.Text + "'");
            else if (chCurrent.Checked)
                sb.AppendLine("      ,'current database'");

            if (cbContract.Text != string.Empty)
                sb.AppendLine("      ON CONTRACT [" + cbContract.Text + "]");

            bool jest = false;

            if (cbConversGroup.Text != string.Empty || cbConversation.Text != string.Empty)
            {

                sb.AppendLine("      WITH");
                if (cbConversation.Text != string.Empty)
                {
                    jest = true;
                    sb.AppendLine("         RELATED_CONVERSATION = cast('" + cbConversation.Text + "' as UNIQUEIDENTIFIER)");
                }
                else if (cbConversGroup.Text != string.Empty)
                {
                    jest = true;
                    sb.AppendLine("         RELATED_CONVERSATION_GROUP = cast('" + cbConversGroup.Text + "' as UNIQUEIDENTIFIER)");
                }

                if (tbLifetime.Text != string.Empty)
                {
                    if (jest)
                    {
                        sb.AppendLine("         ,");
                    }
                    jest = true;
                    sb.AppendLine("         LIFETIME = " + tbLifetime.Text);
                }
                if (!chEncrypt.Checked)
                {
                    if (jest)
                    {
                        sb.AppendLine("         ,");
                    }
                    jest = true;
                    sb.AppendLine("         ENCRYPTION = OFF;");
                }
                else
                {
                    if (jest)
                    {
                        sb.AppendLine("         ,");
                    }
                    jest = true;
                    sb.AppendLine("         ENCRYPTION = ON;");
                }

            }
            else
            {
                if (tbLifetime.Text != string.Empty)
                {
                    jest = true;
                    sb.AppendLine("         WITH LIFETIME = " + tbLifetime.Text);
                }

                if (!chEncrypt.Checked)
                {
                    if (jest)
                        sb.AppendLine("            ,ENCRYPTION = OFF;");
                    else
                        sb.AppendLine("         WITH ENCRYPTION = OFF;");
                }
                else
                {
                    if (jest)
                        sb.AppendLine("            ,ENCRYPTION = ON;");
                    else
                        sb.AppendLine("         WITH ENCRYPTION = ON;");
                }
            }

            if (tbSend.Text != string.Empty)
                sb.AppendLine("      SET @msg = N'" + tbSend.Text.Replace("'", "''") + "';");

            sb.AppendLine("      SEND ON CONVERSATION @ch ");
            if (cbMessage.Text != string.Empty)
                sb.AppendLine("      MESSAGE TYPE [" + cbMessage.Text + "] ");
            if (tbSend.Text != string.Empty)
                sb.AppendLine("      (@msg);");

            sb.AppendLine("   COMMIT TRANSACTION");
            sb.AppendLine("END TRY");
            sb.AppendLine("BEGIN CATCH");
            sb.AppendLine("   ROLLBACK TRANSACTION;");
            sb.AppendLine("   DECLARE @m nvarchar(max)");
            sb.AppendLine("   SET @m = error_message()");
            sb.AppendLine("   RAISERROR('%s',16,1,@m)");
            sb.AppendLine("END CATCH");

            tbCommand.Text = sb.ToString();
        }

        private void chCurrent_CheckedChanged(object sender, EventArgs e)
        {
            if (chCurrent.Checked)
            {
                lblGuid.Enabled = false;
                tbGuid.Text = "";
                tbGuid.Enabled = false;
            }
            else
            {
                lblGuid.Enabled = true;
                tbGuid.Enabled = true;
            }

            Write();
        }

        private void cbContract_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void chEncrypt_CheckedChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbLifetime_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbTo_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbGuid_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbMessage_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbSend_TextChanged(object sender, EventArgs e)
        {
            lblCounter.Text = string.Format("Message length: {0}", tbSend.Text.Length);
            Write();
        }

        private void cbConversation_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbConversGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            string g = Guid.NewGuid().ToString().ToUpper();

            cbConversGroup.Items.Add(g);
            cbConversGroup.SelectedText = "";
            cbConversGroup.SelectedText = g;

            Write();

        }

        private void cbServices_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }
    }
}